CMakeLists 生成动态库与静态库
文件结构
1 | ├── bin |
最后生成的库文件将会放在lib目录下,并且生成的库文件带版本号
顶层CMakeLists.txt
1
2
3cmake_minimum_required(VERSION 3.3)
project(libraryTest CXX)
add_subdirectory(src)src/CMakeLists.txt
1
2
3# src CMakeLists.txt
add_subdirectory(main)
add_subdirectory(person)src/main/CMakeLists.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15# contacts CMakeLists.txt
aux_source_directory(. srcs)
include_directories(${PROJECT_SOURCE_DIR}/include )
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
add_executable( librarytest ${srcs})
link_directories(${PROJECT_SOURCE_DIR}/lib)
target_link_libraries(librarytest
person
)src/person/CMakeLists.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25aux_source_directory(. srcs)
include_directories(${PROJECT_SOURCE_DIR}/include )
set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)
# 之前的添加动态库/静态库的方法
# 缺点:动态库与静态库的名字不能重名
# add_library(person SHARED ${srcs})
# 生成动态库目标
add_library(person SHARED ${srcs})
# 生成静态库目标
add_library(person_static STATIC ${srcs})
# 指定静态库的输出名称
set_target_properties(person_static PROPERTIES OUTPUT_NAME "person")
# 使动态库和静态库同时存在
set_target_properties(person PROPERTIES CLEAN_DIRECT_OUTPUT 1)
set_target_properties(person_static PROPERTIES CLEAN_DIRECT_OUTPUT 1)
# 指定动态库版本
# VERSION 动态库版本
# SOVERSION API版本
set_target_properties(person PROPERTIES VERSION 1.0 SOVERSION 1)运行
1
2
3cd build
cmake ..
make